package com.hs.bai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.bai.mapper.*;
import com.hs.bai.pojo.AdditionalGoodsinfo;
import com.hs.bai.pojo.Goodsinfo;
import com.hs.bai.service.IOrderService;
import com.hs.bai.pojo.Order;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author Dingkun Bai
 * @since 2022-06-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderbasicinfoMapper orderbasicinfoMapper;
    @Autowired
    private AddrinfoMapper addrinfoMapper;
    @Autowired
    private ExpenseinfoMapper expenseinfoMapper;
    @Autowired
    private GoodsinfoMapper goodsinfoMapper;
    @Autowired
    private AdditionalGoodsinfoMapper additionalGoodsinfoMapper;

    @Override
    public ResultBean deleteAllInfo(List<String> ids) {

        //删除基本表
        int i1 = orderbasicinfoMapper.deleteBatchIds(ids);

        //删除地址表
        ids.forEach(id -> {
            Order order = orderMapper.selectById(id);
            addrinfoMapper.deleteById(order.getTAId());
        });

        //删除货物信息总表
        ids.forEach(id -> {

            QueryWrapper<AdditionalGoodsinfo> qg = new QueryWrapper<>();
            qg.eq("order_id",id);
            additionalGoodsinfoMapper.delete(qg);

        });

        //删除货物信息表
        ids.forEach(id -> {

            QueryWrapper<Goodsinfo> qg = new QueryWrapper<>();
            qg.eq("order_id",id);
            goodsinfoMapper.delete(qg);

        });

        //删除价格信息表
        ids.forEach(id -> {

            Order order = orderMapper.selectById(id);
            expenseinfoMapper.deleteById(order.getTFId());

        });

        //删除总表
        int i = orderMapper.deleteBatchIds(ids);


        return ResultBean.OK("删除成功！");
    }
}
